package com.jlnk.androiduidemo;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.jlnk.androiduidemo.Adapter.CourseArrayAdapter;
import com.jlnk.androiduidemo.database.CourseMessage;
import com.jlnk.androiduidemo.entity.Course;

import java.util.List;

public class CourseActivity extends AppCompatActivity {
    private ListView listView;
    private CourseMessage dbHelper;
    private List<Course> courses;
    private CourseArrayAdapter courseArrayAdapter;
    private Button buttonStudents, buttonProfile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course);

        dbHelper = new CourseMessage(this);
        listView = findViewById(R.id.course_list);

        // 获取所有课程信息
        courses = dbHelper.getAllCourses();
        courseArrayAdapter = new CourseArrayAdapter(this, courses);
        listView.setAdapter(courseArrayAdapter);

        // 初始化学生按钮，并设置点击事件监听器
        buttonStudents = findViewById(R.id.buttonStudents);
        buttonStudents.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent courseIntent = new Intent(CourseActivity.this, StudentActivity.class);
                startActivity(courseIntent);
            }
        });

        // 初始化个人资料按钮，并设置点击事件监听器
        buttonProfile = findViewById(R.id.buttonProfile);
        buttonProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String studentId = getCurrentStudentId(); // 获取当前学生的学号
                if (studentId != null) {
                    // 如果学号存在，创建Intent并传递学号，跳转到个人信息界面
                    Intent profileIntent = new Intent(CourseActivity.this, InformationActivity.class);
                    profileIntent.putExtra("studentId", studentId);
                    startActivity(profileIntent);
                } else {
                    // 如果学号不存在，提示用户重新登录
                    Toast.makeText(CourseActivity.this, "未能获取学号，请重新登录", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    // 获取当前登录学生的学号
    private String getCurrentStudentId() {
        SharedPreferences sharedPreferences = getSharedPreferences("user_session", MODE_PRIVATE);
        return sharedPreferences.getString("studentId", null);
    }
}